import os
import re
import subprocess
import time
from concurrent.futures.thread import ThreadPoolExecutor


from utils.Sun_json import Sun_json
from utils.Sun_os import Sun_os
from utils.Sun_tkinter import Sun_tkinter
from utils.Sun_uiautomator2 import Sun_uiautomator2



thread_pool_obj = ThreadPoolExecutor()

# tk主窗口全局变量========================================================
# 窗口的进程pid 多个窗口保存log时，用pid区分不同窗口的log
current_tk_window_pid = None
# 设备连接状态标志位 为false时 所有按钮不可用  为true时 所有按钮功能正常使用
connect_status = False
# 设备连接中状态 标志位  为true时 设备不能再次连接
connecting_status = False
# 保存设备连接成功时的设备sn号
connected_sn = None
# 设备连接成功时的目标设备对象 通过此对象可以对设备进行相关操作
connected_object = None

# 抓取设备log标志位 为false时 log正常抓取  为true时 不能进行log抓取
catch_log_status = False
# 抓取设备log的进程id 停止抓log时用
catch_log_pid = None

# 录屏标志位 为false时 正常录屏  为true时 不能进行录屏
record_screen_status = False
# 录屏的进程id 停止录屏时用
record_screen_pid = None
# 录屏保存位置
recording_screen_save_path = None

# 保存程序切换工作目录前的目录
switch_working_path_before = None

# 用户选择的设备sn
user_selected_device_sn = None

# 执行命令状态标志位 为false命令正常执行 为true时 不能执行命令
execute_command_status = False
# 保存当前正在执行的命令
current_execute_command = None
# 保存用户输入的命令
user_input_command = None

# 保存用户输入的包名
user_input_package = None

# 标记是否在安装app的状态中 为true时 不能安装app
app_installing_status = False

# monkey测试子窗口全局变量 ====================================================================
# monkey命令执行状态标志位 为false monkey命令正常执行 为true时 不能执行monkey命令
monkey_execute_status = False
# monkey执行的各项事件参数 可选的占比
monkey_para_percent = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# monkey执行的每个事件间隔 可选的间隔长度
event_timeout_time = [300,500,1000, 2000, 3000]
# monkey执行的事件个数  可选的事件个数
event_number = [10, 50, 100, 300, 500, 1000, 3000, 7000, 10000, 30000, 70000, 100000]

# 用户输入的触摸事件占比
user_input_touch_event_percent = None
# 用户输入的水平滑动事件占比
user_input_straight_slide_event_percent = None
# 用户输入的曲线滑动事件占比
user_input_bend_slide_event_percent = None
# 用户输入的屏幕缩放事件占比
user_input_window_small_event_percent = None
# 用户输入的屏幕旋转事件占比
user_input_window_turn_event_percent = None
# 用户输入的系统按键事件占比
user_input_system_key_event_percent = None
# 用户输入的键盘输入事件占比
user_input_keyboard_event_percent = None
# 用户输入的页面切换事件占比
user_input_switch_page_event_percent = None
# 用户输入的事件间隔时长
user_input_event_timeout = None
# 用户输入的事件个数
user_input_event_count = None

# 解决在任意位置可正常运行打包后的exe程序
def init_exe_path():
    global switch_working_path_before
    default_working_path = os.path.dirname(os.path.abspath(__file__))
    print(f"default_working_path={default_working_path}")
    switch_working_path_before = os.getcwd()
    print(f"switch_working_path_before={switch_working_path_before}")
    os.chdir(default_working_path)
    switch_working_path_after = os.getcwd()
    print(f"switch_working_path_after={switch_working_path_after}")

# 初始化主窗口
def init_main_tk_window_implement(tk_window,tk_style):
    global switch_working_path_before
    tk_handle.set_window_title(tk_window, "adb界面调试工具")
    tk_handle.set_window_size(tk_window, 883, 1010, 300, 150)
    tk_handle.set_window_size_lock_status(tk_window, False, False)
    # tk_handle.set_window_icon(tk_window, ".\\tk_ico\\tk_exe.ico")
    # 常用主题
    # ['cosmo', 'flatly', 'litera', 'minty', 'lumen', 'sandstone', 'yeti', 'pulse', 'united', 'morph', 'journal',
    # 'darkly', 'superhero', 'solar', 'cyborg', 'vapor', 'simplex', 'cerculean']
    tk_style.theme_use("pulse")
    os.chdir(switch_working_path_before)


# 日志打印区域
log_print_area_obj = None
def log_print_area_implement():
    global log_print_area_obj
    log_print_area_obj = tk_handle.create_ttk_text(tk_window,font=10,width=86,height=30)
    tk_handle.hand_tk_element_place(log_print_area_obj, 5, 350)


# 清除日志
def clear_log_implement():
    global log_print_area_obj
    def div_text_clear_log_button_callback():
        tk_handle.clear_data_in_tk_text(log_print_area_obj)
        print("清除日志")
    div_text_clear_log_button = tk_handle.create_ttk_button(tk_window, "清除日志",bootstyle="dark",width=9,callback=div_text_clear_log_button_callback)
    tk_handle.hand_tk_element_place(div_text_clear_log_button, 5,968)

# 导出日志
def export_log_implement():
    global log_print_area_obj
    def div_text_download_log_button_callback():
        current_tk_window_pid = os.getpid()
        div_text_data = tk_handle.get_text_input_data(log_print_area_obj)
        if div_text_data == "\n":
            print("当前窗口没有日志")
            tk_handle.pop_err_tip("错误", "当前窗口没有日志,导出失败")
        else:
            current_time = os_handle.get_current_time()
            tk_log_save_dir = f"D:\\tk_log\\"
            if os_handle.check_dirfile_is_exist(tk_log_save_dir) == False:
                os_handle.make_dir(tk_log_save_dir)
            tk_log_save_path = f"D:\\tk_log\\tk_pid_{current_tk_window_pid}_{current_time}_log.txt"
            os_handle.make_file(tk_log_save_path)
            file = open(tk_log_save_path, "w")
            file.write(div_text_data)
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前窗口展示日志导出成功,保存在{tk_log_save_path}位置\n")
    div_text_download_log_button = tk_handle.create_ttk_button(tk_window, "导出日志",bootstyle="dark",width=9,callback=div_text_download_log_button_callback)
    tk_handle.hand_tk_element_place(div_text_download_log_button, 130,968)


# 选择设备标签
def select_device_label_implement():
    select_device_label = tk_handle.create_ttk_label(tk_window, "选择设备:",bootstyle="dark",text_size=12,width=10)
    tk_handle.hand_tk_element_place(select_device_label, 10, 5)

# 被测包名标签
def package_name_input_label_implement():
    input_app_pid_label = tk_handle.create_ttk_label(tk_window, "被测包名:",bootstyle="dark",text_size=12,width=10)
    tk_handle.hand_tk_element_place(input_app_pid_label, 10, 175)

# 输入命令标签
def command_input_label_implement():
    input_command_label = tk_handle.create_ttk_label(tk_window, "输入命令:",bootstyle="dark",text_size=12,width=10)
    tk_handle.hand_tk_element_place(input_command_label, 10, 305)




# 下拉选择设备
def select_device_dropbox_implement():
    global user_selected_device_sn
    user_selected_device_sn = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_selected_device_sn, "请点击选择设备")
    dic = json_handle.read_json_file(".\\connected_device_ip_port.json")
    connected_devices_list = dic["connected_devices"]

    dropbox = tk_handle.create_ttk_dropbox(tk_window, options=connected_devices_list, bind_str=user_selected_device_sn,width=32,text_size=9,bootstyle=("dark", "outline-toolbutton"))
    tk_handle.hand_tk_element_place(dropbox, 128, 5)

# 包名输入框
def package_name_input_entry_implement():
    global user_input_package
    user_input_package = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_package, "com.sohu.inputmethod.sogou")
    package_input_entry = tk_handle.create_ttk_entry(tk_window, bind_str=user_input_package, width=33,text_size=9,bootstyle="dark")
    tk_handle.hand_tk_element_place(package_input_entry, 128, 175)


# 命令输入框
def command_input_entry_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_command
    def entry_inter_callback(event):
        global execute_command_status
        global current_execute_command
        if connect_status == False:
            print(f"execute_command_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if execute_command_status == True:
                print(f"execute_command_button_callback-----上一条命令{current_execute_command}未执行完毕,等待执行完,再继续执行")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备,上一条命令{current_execute_command}未执行完毕,等待执行完,再继续执行,[{tk_handle.get_tk_string_value(user_input_command)}]命令执行失败\n")
            else:
                if tk_handle.get_tk_string_value(user_input_command) == "":
                    print(f"execute_command_button_callback-----请输入命令")
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备,请输入命令\n")
                else:
                    def execute_command_button_callback_execute_thread():
                        global execute_command_status
                        global current_execute_command
                        current_execute_command = tk_handle.get_tk_string_value(user_input_command)
                        execute_long_command(f'adb -s {connected_sn} shell "{current_execute_command}"')
                    def get_execute_command_pid():
                        global execute_command_status
                        global current_execute_command
                        start_time = time.time()
                        find_execute_command_pid_command = 'adb -s ' + connected_sn + ' shell \" ps -elf | grep ^shell | grep ' + '\'' + tk_handle.get_tk_string_value(user_input_command) + '\'' + '   |  grep -v grep | tail -n 1 | awk -F \' \' \'{print $2}\'\"'
                        print(f"execute_command_button_callback-----find_execute_command_pid_command={find_execute_command_pid_command}")
                        while True:
                            execute_result = os_handle.execute_short_command(find_execute_command_pid_command)
                            print(f'execute_command_button_callback-----execute_result={execute_result}')
                            if execute_result != "":
                                global execute_command_pid
                                execute_command_pid = execute_result
                                print(f"execute_command_button_callback-----execute_command_pid={execute_command_pid}")
                                return
                            if time.time() - 1 > start_time:
                                print(f"execute_command_button_callback-----退出循环，未找到进程id,执行的是短命令")
                                return
                    thread_pool_obj.submit(execute_command_button_callback_execute_thread)
                    thread_pool_obj.submit(get_execute_command_pid)
    user_input_command = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_command, "ping baidu.com")
    input_entry = tk_handle.create_ttk_entry(tk_window, bind_str=user_input_command, width=44,bootstyle="dark",text_size=9)
    tk_handle.hand_tk_element_place(input_entry, 129, 305)
    input_entry.bind("<Return>", entry_inter_callback)



# 连接设备
def connect_device_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def connect_button_callback():
        if tk_handle.get_tk_string_value(user_selected_device_sn) == "":
            print("connect_button_callback-----未选择设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, "未选择设备\n")
        else:
            current_connect_device = tk_handle.get_tk_string_value(user_selected_device_sn)
            print(f"connect_button_callback-----current_connect_device={current_connect_device}")
            global connecting_status
            if connecting_status:
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"请等待上次连接结果后,再进行连接\n")
                return
            tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{current_connect_device}]设备连接中......\n")
            connecting_status = True
            def connect_button_callback_execute_thread():
                global connected_sn
                global catch_log_status
                global record_screen_status
                global connect_status
                global connected_object
                global connecting_status
                try:
                    connect_result = u2_handle.connect_device(tk_handle.get_tk_string_value(user_selected_device_sn))
                except Exception as e:
                    print(f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备失败:{e}")
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{current_connect_device}]设备连接失败,失败原因:\n")
                    tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{current_connect_device}]{e}\n")
                    connect_status = False
                    connecting_status = False
                    connected_object = None
                    connected_sn = None
                    catch_log_status = False
                    record_screen_status = False
                else:
                    print(f"connect_button_callback-----connect_result={connect_result}")
                    if connect_result != None:
                        print(f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备连接成功")
                        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{current_connect_device}]设备连接成功\n")
                        connect_status = True
                        connecting_status = False
                        connected_object = connect_result
                        connected_sn = tk_handle.get_tk_string_value(user_selected_device_sn)
                    else:
                        print(f"connect_button_callback-----[{current_connect_device}]设备连接失败")
                        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{current_connect_device}]设备连接失败\n")
            thread_pool_obj.submit(connect_button_callback_execute_thread)
    connect_button = tk_handle.create_ttk_button(tk_window, "连接",bootstyle=("dark"),callback=connect_button_callback,width=9)
    tk_handle.hand_tk_element_place(connect_button, 505, 5)


# 功能部分=====================================================================================================
# 截屏
def shot_screen_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def shot_screen_button_callback():
        if connect_status == False:
            print(f"shot_screen_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备截屏中......\n")
            def shot_screen_button_callback_execute_thread():
                if connected_sn.startswith("192.168."):
                    shot_screen_save_path = f"D:\\{connected_sn[0:-5]}"
                else:
                    shot_screen_save_path = f"D:\\{connected_sn}"
                if os_handle.check_dirfile_is_exist(shot_screen_save_path) == False:
                    os_handle.make_dir(shot_screen_save_path)
                execute_result = u2_handle.screen_shot_device_screen(connected_object, shot_screen_save_path)
                print(f"shot_screen_button_callback-----execute_result={execute_result}")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{connected_sn}]设备截图成功，截图保存在{shot_screen_save_path}目录\n")
            thread_pool_obj.submit(shot_screen_button_callback_execute_thread)
    shot_screen_button = tk_handle.create_ttk_button(tk_window, "截屏",bootstyle=("dark", "outline"),width=9,callback=shot_screen_button_callback)
    tk_handle.hand_tk_element_place(shot_screen_button, 5, 50)


# 清除设备log缓存
def clear_device_log_buffer_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def clear_log_button_callback():
        if connect_status == False:
            print(f"clear_log_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备清除log缓存中......\n")
            def clear_log_button_callback_execute_thread():
                execute_command = f'adb -s {connected_sn} shell "logcat -c"'
                execute_result = os_handle.execute_short_command(execute_command)
                print(f"clear_log_button_callback-----execute_result={execute_result}")
                tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{connected_sn}]设备清除log缓存完毕\n")
            thread_pool_obj.submit(clear_log_button_callback_execute_thread)
    clear_log_button = tk_handle.create_ttk_button(tk_window, "清日志缓存",bootstyle=("dark", "outline"),width=9,callback=clear_log_button_callback)
    tk_handle.hand_tk_element_place(clear_log_button, 130, 50)


# 修改根目录只读权限
def remount_readonly_file_system_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def update_readonly_button_callback():
        if connect_status == False:
            print(f"update_readonly_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            print(f"update_readonly_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备修改只读权限中......")
            def update_readonly_button_callback_execute_thread():
                execute_command = f'adb -s {connected_sn} shell "mount -o remount,rw /sdcard/"'
                execute_result = os_handle.execute_short_command(execute_command)
                print(f"update_readonly_button_callback-----execute_result={execute_result}")
                if execute_result == "":
                    print(f"update_readonly_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备修改只读权限成功")
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备修改只读权限成功\n')
                else:
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备修改只读权限失败,{execute_result}\n")
            thread_pool_obj.submit(update_readonly_button_callback_execute_thread)
    update_readonly_button = tk_handle.create_ttk_button(tk_window, "改只读权限",bootstyle=("dark", "outline"),width=9,callback=update_readonly_button_callback)
    tk_handle.hand_tk_element_place(update_readonly_button, 505,50)


# 清除crash文件
def clear_crash_file_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def clear_crash_button_callback():
        if connect_status == False:
            print(f"clear_crash_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备清除crash文件中......\n')
            def clear_crash_button_callback_execute_thread():
                crash_path = "/sdcard/sunbo/"
                execute_command = f'adb -s {connected_sn} shell "rm -rf {crash_path}*.txt"'
                execute_result = os_handle.execute_short_command(execute_command)
                print(f"clear_crash_button_callback-----execute_result={execute_result}")
                print(f"clear_crash_button_callback-----清除crash文件成功")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备清除crash文件成功\n')
            thread_pool_obj.submit(clear_crash_button_callback_execute_thread)
    clear_crash_button = tk_handle.create_ttk_button(tk_window, "清空Crash",bootstyle=("dark", "outline"),width=9,callback=clear_crash_button_callback)
    tk_handle.hand_tk_element_place(clear_crash_button, 630, 50)


# 获取crash文件
def get_crash_file_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def get_crash_button_callback():
        if connect_status == False:
            print(f"get_crash_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取crash文件中......\n')
            def get_crash_button_callback_execute_thread():
                crash_path = "/vendor/etc/"
                if connected_sn.startswith("192.168."):
                    crash_save_path = f"D:\\{connected_sn[0:-5]}\\"
                else:
                    crash_save_path = f"D:\\{connected_sn}\\"
                print(f"get_crash_button_callback-----crash_save_path={crash_save_path}")
                if os_handle.check_dirfile_is_exist(crash_save_path) == False:
                    os_handle.make_dir(crash_save_path)
                execute_command = f'adb -s {connected_sn} pull {crash_path} {crash_save_path}'
                execute_result = os_handle.execute_short_command(execute_command)
                print(f"get_crash_button_callback-----execute_result={execute_result}")
                print(f"get_crash_button_callback-----获取crash文件成功")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取crash文件成功,保存在{crash_save_path}目录\n')
            thread_pool_obj.submit(get_crash_button_callback_execute_thread)
    get_crash_button = tk_handle.create_ttk_button(tk_window, "获取Crash",bootstyle=("dark", "outline"),width=9,callback=get_crash_button_callback)
    tk_handle.hand_tk_element_place(get_crash_button, 755, 50)

# 当前包名
def get_current_package_message_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def get_package_name_button_callback():
        if connect_status == False:
            print(f"get_package_name_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取当前展示的包名_activity_进程id_信息中......\n')
            def get_package_name_button_callback_execute_thread():
                execute_result = u2_handle.get_current_show_apk(connected_object)
                print(f"get_package_name_button_callback-----execute_result={execute_result}")
                print(f"get_package_name_button_callback-----获取包名成功")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取当前展示的包名_activity_进程id_信息成功,信息如下:\n')
                tk_handle.add_data_in_tk_text(log_print_area_obj, f'{execute_result}\n')
            thread_pool_obj.submit(get_package_name_button_callback_execute_thread)
    get_package_name_button = tk_handle.create_ttk_button(tk_window, "当前包名",bootstyle=("dark", "outline"),width=9,callback=get_package_name_button_callback)
    tk_handle.hand_tk_element_place(get_package_name_button, 5,90)


# 开始录屏
def start_recording_implement():
    global log_print_area_obj
    def start_record_screen_callback():
        global recording_screen_save_path
        if connect_status == False:
            print(f"start_record_screen_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            global record_screen_status
            if record_screen_status:
                print(f"start_record_screen_callback-----当前正在录屏中,请先停止")
                tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{connected_sn}]设备当前正在录屏中,请先停止\n")
                return
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{connected_sn}]设备当前正在录屏中......\n")
            record_screen_status = True
            current_time = os_handle.get_current_time()
            if connected_sn.startswith("192.168."):
                recording_screen_save_path = f"/sdcard/{connected_sn[0:-5]}_recording_screen_{current_time}.mp4"
            else:
                recording_screen_save_path = f"/sdcard/{connected_sn}_recording_screen_{current_time}.mp4"
            print(f"start_record_screen_callback-----save_path={recording_screen_save_path}")
            def recording_screen_thread():
                execute_command = f'adb -s {connected_sn} shell "screenrecord {recording_screen_save_path} "'
                os_handle.execute_short_command(execute_command)

            def find_recording_pid_execute_thread():
                find_recording_pid_command = 'adb -s ' + connected_sn + ' shell \" ps -elf | grep screenrecord | grep ^shell |  grep -v grep | tail -n 1 | awk -F \' \' \'{print $2}\'\"'
                print(f"start_record_screen_callback-----find_recording_pid_command={find_recording_pid_command}")
                while True:
                    global record_screen_pid
                    execute_result = os_handle.execute_short_command(find_recording_pid_command)
                    if execute_result != "":
                        record_screen_pid = execute_result
                        print(f"start_record_screen_callback-----record_screen_pid={record_screen_pid}")
                        return
            thread_pool_obj.submit(find_recording_pid_execute_thread)
            thread_pool_obj.submit(recording_screen_thread)
    start_record_screen_button = tk_handle.create_ttk_button(tk_window, "开始录屏",bootstyle=("dark", "outline"),width=9,callback=start_record_screen_callback)
    tk_handle.hand_tk_element_place(start_record_screen_button, 130, 90)

# 停止录屏
def stop_recording_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def stop_record_screen_button_callback():
        global connect_status
        global connected_sn
        global record_screen_status
        if connect_status == False:
            print(f"stop_record_screen_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if record_screen_status == False:
                print(f"stop_record_screen_button_callback-----当前没有录屏")
                tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{connected_sn}]设备当前没有录屏\n")
            else:
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止录屏中......\n")
                def stop_record_screen_button_callback_execute_thread():
                    global record_screen_status
                    global record_screen_pid
                    print(f"stop_record_screen_button_callback-----record_screen_pid={record_screen_pid}")
                    execute_result_kill_record_pid = os_handle.execute_short_command(f'adb -s {connected_sn} shell "kill -2 {record_screen_pid}"')
                    print(f"stop_record_screen_button_callback-----execute_result_kill_record_pid={execute_result_kill_record_pid}")
                    if "No such process" in execute_result_kill_record_pid:
                        print(f"stop_record_screen_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止录屏失败")
                        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止录屏失败\n")
                    else:
                        time.sleep(1)
                        recorded_save_path = recording_screen_save_path
                        if connected_sn.startswith("192.168."):
                            save_path = f"D:\\{connected_sn[0:-5]}\\"
                        else:
                            save_path = f"D:\\{connected_sn}\\"
                        if os_handle.check_dirfile_is_exist(save_path) == False:
                            os_handle.make_dir(save_path)
                        execute_result_get_record = os_handle.execute_short_command(f"adb -s {connected_sn} pull {recorded_save_path} {save_path}")
                        print(f"stop_record_screen_button_callback-----execute_result_get_record={execute_result_get_record}")
                        print(f"stop_record_screen_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止录屏成功")
                        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止录屏成功,录屏保存在{save_path}目录\n")
                        record_screen_status = False
                thread_pool_obj.submit(stop_record_screen_button_callback_execute_thread)
    stop_record_screen_button = tk_handle.create_ttk_button(tk_window, "停止录屏",bootstyle=("dark", "outline"),width=9,callback=stop_record_screen_button_callback)
    tk_handle.hand_tk_element_place(stop_record_screen_button, 255,90)



# 开始抓全量log
def start_catch_log_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def catch_log_button_callback():
        global catch_log_status
        global catch_log_pid
        if connect_status == False:
            print(f"catch_log_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            print(f"catch_log_button_callback-----[{connected_sn}]设备开始抓全量log中.....")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{connected_sn}]设备抓取全量log中......\n")
            if catch_log_status:
                print(f"catch_log_button_callback-----当前正在抓log中,请先停止")
                tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{connected_sn}]设备当前正在抓log中,请先停止\n")
                return
            def catch_log_button_callback_execute_thread():
                global catch_log_status
                current_time = os_handle.get_current_time()
                if connected_sn.startswith("192.168."):
                    log_save_path = f"D:\\{connected_sn[0:-5]}\\{current_time}_log.txt"
                else:
                    log_save_path = f"D:\\{connected_sn}\\{current_time}_log.txt"
                print(f"catch_log_button_callback-----save_path={log_save_path}")
                if os_handle.check_dirfile_is_exist(os_handle.get_dirname_in_path(log_save_path)) == False:
                    os_handle.make_dir(os_handle.get_dirname_in_path(log_save_path))
                catch_log_status = True
                execute_command = f'adb -s {connected_sn} shell "logcat" > {log_save_path}'
                os_handle.execute_short_command(execute_command)

            def get_catch_log_pid_execute_thread():
                find_catch_log_pid_command = 'adb -s ' + connected_sn + ' shell \" ps -elf | grep -w ^shell.*logcat  |  grep -v grep | tail -n 1 | awk -F \' \' \'{print $2}\'\"'
                print(f"catch_log_button_callback-----find_catch_log_pid_command={find_catch_log_pid_command}")
                while True:
                    global catch_log_pid
                    execute_result = os_handle.execute_short_command(find_catch_log_pid_command)
                    if execute_result != "":
                        catch_log_pid = execute_result
                        print(f"catch_log_button_callback-----catch_log_pid={catch_log_pid}")
                        return
            thread_pool_obj.submit(get_catch_log_pid_execute_thread)
            thread_pool_obj.submit(catch_log_button_callback_execute_thread)
    catch_log_button = tk_handle.create_ttk_button(tk_window, "开始抓日志",bootstyle=("dark", "outline"),width=9,callback=catch_log_button_callback)
    tk_handle.hand_tk_element_place(catch_log_button, 255, 50)

# 停止抓全量log
def stop_catch_log_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def stop_log_button_callback():
        global connect_status
        global connected_sn
        global catch_log_status
        if connect_status == False:
            print(f"stop_log_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if catch_log_status == False:
                print(f"stop_log_button_callback-----当前没有抓日志")
                tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{connected_sn}]设备当前没有抓日志\n")
            else:
                print(f"stop_log_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止抓log中......")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止抓log中......\n")
                def stop_log_button_callback_execute_thread():
                    global catch_log_status
                    global catch_log_pid
                    print(f"stop_log_button_callback-----catch_log_pid={catch_log_pid}")
                    execute_result = os_handle.execute_short_command(f'adb -s {connected_sn} shell "kill -2  {catch_log_pid}"')
                    if "No such process" in execute_result:
                        print(f"stop_log_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止抓取全量log失败")
                        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止抓log失败\n")
                    else:
                        time.sleep(1)
                        if connected_sn.startswith("192.168."):
                            log_save_path = f"D:\\{connected_sn[0:-5]}\\"
                        else:
                            log_save_path = f"D:\\{connected_sn}\\"
                        print(f"stop_log_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止抓log成功")
                        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止抓取全量log成功,日志保存在{log_save_path}目录\n")
                        catch_log_status = False
                thread_pool_obj.submit(stop_log_button_callback_execute_thread)
    stop_log_button = tk_handle.create_ttk_button(tk_window, "停止抓日志",bootstyle=("dark", "outline"),width=9,callback=stop_log_button_callback)
    tk_handle.hand_tk_element_place(stop_log_button, 380, 50)



# 获取设备信息
def get_device_system_info_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def get_device_message_button_callback():
        if connect_status == False:
            print(f"get_package_name_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取系统参数信息中......\n')
            def get_device_message_button_callback_execute_thread():
                execute_result = u2_handle.get_device_info(connected_object)
                print(f"get_package_name_button_callback-----execute_result={execute_result}")
                print(f"get_package_name_button_callback-----获取设备信息成功")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取系统参数信息成功,信息如下:\n')
                tk_handle.add_data_in_tk_text(log_print_area_obj, f'{execute_result}\n')
            thread_pool_obj.submit(get_device_message_button_callback_execute_thread)
    get_device_message_button = tk_handle.create_ttk_button(tk_window, "系统信息",bootstyle=("dark", "outline"),width=9,callback=get_device_message_button_callback)
    tk_handle.hand_tk_element_place(get_device_message_button, 380,90)


# 网络信息
def get_device_net_info_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def get_device_net_message_button_callback():
        if connect_status == False:
            print(f"get_package_name_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取网络信息中......\n')
            def get_device_net_message_button_callback_execute_thread():
                execute_result = os_handle.execute_short_command(f'adb -s {connected_sn} shell "ifconfig"')
                print(f"get_package_name_button_callback-----execute_result={execute_result}")
                print(f"get_package_name_button_callback-----获取设备网络成功")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取网络信息成功,信息如下:\n')
                tk_handle.add_data_in_tk_text(log_print_area_obj, f'{execute_result}\n')
            thread_pool_obj.submit(get_device_net_message_button_callback_execute_thread)
    get_device_net_message_button = tk_handle.create_ttk_button(tk_window, "网络信息",bootstyle=("dark", "outline"),width=9,callback=get_device_net_message_button_callback)
    tk_handle.hand_tk_element_place(get_device_net_message_button, 505,90)


# 三方包列表
def get_third_app_info_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def get_third_app_button_callback():
        if connect_status == False:
            print(f"get_package_name_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取三方app安装信息中......\n')
            def get_third_app_button_callback_execute_thread():
                execute_result = os_handle.execute_short_command(f'adb -s {connected_sn} shell "pm list packages -3"')
                print(f"get_package_name_button_callback-----execute_result={execute_result}")
                print(f"get_package_name_button_callback-----获取三方app安装列表成功")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取三方app安装信息成功,信息如下:\n')
                tk_handle.add_data_in_tk_text(log_print_area_obj, f'{execute_result}\n')
            thread_pool_obj.submit(get_third_app_button_callback_execute_thread)
    get_third_app_button = tk_handle.create_ttk_button(tk_window, "三方包信息",bootstyle=("dark", "outline"),width=9,callback=get_third_app_button_callback)
    tk_handle.hand_tk_element_place(get_third_app_button, 630, 90)


# 执行长命令
def execute_long_command(command):
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_command
    global execute_command_status
    execute_command_status = True
    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备,[{tk_handle.get_tk_string_value(user_input_command)}]命令执行结果:\n")
    result = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,encoding='gbk', shell=True)
    for result_buffer in result.stdout:
        print(result_buffer)
        tk_handle.add_data_in_tk_text(log_print_area_obj, f"{result_buffer}")
    execute_command_status = False


# 执行命令按钮
def start_execute_command_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_command
    def execute_command_button_callback():
        global execute_command_status
        global current_execute_command
        if connect_status == False:
            print(f"execute_command_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if execute_command_status == True:
                print( f"execute_command_button_callback-----上一条命令{current_execute_command}未执行完毕,等待执行完,再继续执行")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备,上一条命令{current_execute_command}未执行完毕,等待执行完,再继续执行,[{tk_handle.get_tk_string_value(user_input_command)}]命令执行失败\n")
            else:
                if tk_handle.get_tk_string_value(user_input_command) == "":
                    print(f"execute_command_button_callback-----请输入命令")
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备,请输入命令\n")
                else:
                    def execute_command_button_callback_execute_thread():
                        global execute_command_status
                        global current_execute_command
                        current_execute_command = tk_handle.get_tk_string_value(user_input_command)
                        execute_long_command(f'adb -s {connected_sn} shell "{current_execute_command}"')
                    def get_execute_command_pid():
                        global execute_command_status
                        global current_execute_command
                        start_time = time.time()
                        find_execute_command_pid_command = 'adb -s ' + connected_sn + ' shell \" ps -elf | grep ^shell | grep ' + '\'' + tk_handle.get_tk_string_value(user_input_command) + '\'' + '   |  grep -v grep | tail -n 1 | awk -F \' \' \'{print $2}\'\"'
                        print(f"execute_command_button_callback-----find_execute_command_pid_command={find_execute_command_pid_command}")
                        while True:
                            execute_result = os_handle.execute_short_command(find_execute_command_pid_command)
                            print(f'execute_command_button_callback-----execute_result={execute_result}')
                            if execute_result != "":
                                global execute_command_pid
                                execute_command_pid = execute_result
                                print(f"execute_command_button_callback-----execute_command_pid={execute_command_pid}")
                                return
                            if time.time() - 1 > start_time:
                                print(f"execute_command_button_callback-----退出循环，未找到进程id,执行的是短命令")
                                return
                    thread_pool_obj.submit(execute_command_button_callback_execute_thread)
                    thread_pool_obj.submit(get_execute_command_pid)
    execute_command_button = tk_handle.create_ttk_button(tk_window, "执行",bootstyle="danger",width=9,callback=execute_command_button_callback)
    tk_handle.hand_tk_element_place(execute_command_button, 630,305)

# 停止执行命令
def stop_execute_command_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def stop_execute_command_button_callback():
        global execute_command_status
        global current_execute_command
        if connect_status == False:
            print(f"stop_execute_command_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if execute_command_status == False:
                print(f"stop_execute_command_button_callback-----未执行命令")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备,不在执行命令中状态\n")
            else:
                print(f"stop_execute_command_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止[{current_execute_command}]命令中......")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止[{current_execute_command}]命令中......\n")
                def stop_execute_command_button_callback_execute_thread():
                    global execute_command_status
                    global execute_command_pid
                    print(f"stop_execute_command_button_callback-----execute_command_pid={execute_command_pid}")
                    execute_result = os_handle.execute_short_command(f'adb -s {connected_sn} shell "kill -2  {execute_command_pid}"')
                    if "No such process" in execute_result:
                        print(f"stop_execute_command_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止执行[{current_execute_command}]命令失败")
                        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止执行[{current_execute_command}]命令失败\n")
                    else:
                        time.sleep(1)
                        print(f"stop_execute_command_button_callback-----[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止执行[{current_execute_command}]命令成功")
                        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备停止执行[{current_execute_command}]命令成功\n")
                        execute_command_status = False
                thread_pool_obj.submit(stop_execute_command_button_callback_execute_thread)
    stop_execute_command_button = tk_handle.create_ttk_button(tk_window, "停止",bootstyle="dark",width=9,callback=stop_execute_command_button_callback)
    tk_handle.hand_tk_element_place(stop_execute_command_button, 755,305)





# cpu信息
def get_app_cpu_info_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_package
    def cpu_button_callback():
        if connect_status == False:
            print(f"cpu_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if tk_handle.get_tk_string_value(user_input_package) == "":
                print(f"cpu_button_callback-----请输入被测包名")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请输入被测包名\n")
            else:
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{tk_handle.get_tk_string_value(user_input_package)}]app获取Cpu信息中......\n')
                def cpu_button_callback_execute_thread():
                    global user_input_package
                    user_input_package_str = tk_handle.get_tk_string_value(user_input_package)
                    print(f"cpu_button_callback_execute_thread-----user_input_package_str={user_input_package_str}")
                    current_device_app_list = os_handle.execute_short_command(f'adb -s {connected_sn} shell "pm list packages -3"').split('\n')
                    print(f"cpu_button_callback_execute_thread-----current_device_app_list={current_device_app_list}")
                    for app_package in current_device_app_list:
                        user_input_package_temp = "package:" + user_input_package_str
                        print(f"cpu_button_callback_execute_thread----user_input_package_temp={user_input_package_temp}")
                        if user_input_package_temp == app_package:
                            print(f"cpu_button_callback_execute_thread----用户输入的包名存在")
                            execute_command = f'adb -s {connected_sn} shell "top -b -n 1 | grep {user_input_package_str} | grep -v grep"'
                            print(f"cpu_button_callback_execute_thread-----execute_command={execute_command}")
                            execute_result = os_handle.execute_short_command(execute_command)
                            print(f"cpu_button_callback_execute_thread-----execute_result={execute_result}")
                            if execute_result == "":
                                print(f"cpu_button_callback-----[{user_input_package_str}]程序不在运行状态")
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中[{user_input_package_str}]app不在运行状态\n")
                                return
                            else:
                                print(f"cpu_button_callback-----execute_result={execute_result}")
                                tk_handle.add_data_in_tk_text(log_print_area_obj, f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app此刻Cpu占用信息如下:\n')
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f' PID  USER         PR   NI VIRT RES  SHR S [%CPU] %MEM   TIME+        ARGS\n')
                                tk_handle.add_data_in_tk_text(log_print_area_obj, f'{execute_result}\n')
                                return
                    print(f"cpu_button_callback_execute_thread-----设备中不存在[{user_input_package_str}]包名")
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中不存在[{user_input_package_str}]包名\n")
                    return
                thread_pool_obj.submit(cpu_button_callback_execute_thread)
    cpu_button = tk_handle.create_ttk_button(tk_window, "Cpu信息",bootstyle=("dark", "outline"),width=9,callback=cpu_button_callback)
    tk_handle.hand_tk_element_place(cpu_button, 5, 220)


# 内存信息
def get_app_memory_info_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_package
    def memory_button_callback():
        if connect_status == False:
            print(f"memory_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if tk_handle.get_tk_string_value(user_input_package) == "":
                print(f"memory_button_callback-----请输入被测包名")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请输入被测包名\n")
            else:
                tk_handle.add_data_in_tk_text(log_print_area_obj, f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{tk_handle.get_tk_string_value(user_input_package)}]app获取内存信息中......\n')
                def memory_button_callback_execute_thread():
                    user_input_package_str = tk_handle.get_tk_string_value(user_input_package)
                    current_device_app_list = os_handle.execute_short_command(f'adb -s {connected_sn} shell "pm list packages -3"').split('\n')
                    for app_package in current_device_app_list:
                        user_input_package_temp = "package:" + user_input_package_str
                        if user_input_package_temp == app_package:
                            execute_result = os_handle.execute_short_command(f'adb -s {connected_sn} shell "dumpsys meminfo {user_input_package_str} | grep Applications -A 25"')
                            if execute_result == "":
                                print(f"memory_button_callback-----[{user_input_package_str}]程序不在运行状态")
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中[{user_input_package_str}]app不在运行状态\n")
                                return
                            else:
                                print(f"memory_button_callback-----execute_result={execute_result}")
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app此刻内存占用信息如下:\n')
                                tk_handle.add_data_in_tk_text(log_print_area_obj, f'{execute_result}\n')
                                return
                    print(f"memory_button_callback-----设备中不存在[{user_input_package}]包名")
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中不存在[{user_input_package_str}]包名\n")
                    return
                thread_pool_obj.submit(memory_button_callback_execute_thread)
    memory_button = tk_handle.create_ttk_button(tk_window, "内存信息",bootstyle=("dark", "outline"),width=9,callback=memory_button_callback)
    tk_handle.hand_tk_element_place(memory_button, 130, 220)


# 电池信息
def get_device_battery_info_implement():
    global log_print_area_obj
    global user_selected_device_sn
    def battery_button_callback():
        if connect_status == False:
            print(f"battery_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取电池信息中......\n')
            def battery_button_callback_execute_thread():
                execute_command = f'adb -s {connected_sn} shell "dumpsys battery"'
                print(f"battery_button_callback-----execute_command={execute_command}")
                execute_result = os_handle.execute_short_command(execute_command)
                print(f"battery_button_callback-----execute_result={execute_result}")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备此刻电池信息如下:\n')
                tk_handle.add_data_in_tk_text(log_print_area_obj, f'{execute_result}\n')
            thread_pool_obj.submit(battery_button_callback_execute_thread)
    battery_button = tk_handle.create_ttk_button(tk_window, "电池信息",bootstyle=("dark", "outline"),width=9,callback=battery_button_callback)
    tk_handle.hand_tk_element_place(battery_button, 755, 90)

# 帧率信息
def get_app_fps_info_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_package
    def fps_button_callback():
        if connect_status == False:
            print(f"fps_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if tk_handle.get_tk_string_value(user_input_package) == "":
                print(f"fps_button_callback-----请输入被测包名")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请输入被测包名\n")
            else:
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{tk_handle.get_tk_string_value(user_input_package)}]app获取帧率信息中......\n')
                def fps_button_callback_execute_thread():
                    user_input_package_str = tk_handle.get_tk_string_value(user_input_package)
                    current_device_app_list = os_handle.execute_short_command(
                        f'adb -s {connected_sn} shell "pm list packages -3"').split('\n')
                    for app_package in current_device_app_list:
                        user_input_package_temp = "package:" + user_input_package_str
                        if user_input_package_temp == app_package:
                            execute_command = 'adb -s ' + connected_sn + ' shell "dumpsys gfxinfo ' + user_input_package_str + ' reset | grep ' + '\'' + 'Graphics info for pid' + '\'' + ' -A 16' + '\"'
                            print(f"fps_button_callback-----execute_command={execute_command}")
                            execute_result = os_handle.execute_short_command(execute_command)
                            if execute_result == "":
                                print(f"fps_button_callback-----[{user_input_package_str}]程序不在运行状态")
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中[{user_input_package_str}]app不在运行状态\n")
                                return
                            else:
                                print(f"fps_button_callback-----execute_result={execute_result}")
                                tk_handle.add_data_in_tk_text(log_print_area_obj, f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app帧率信息如下:\n')
                                tk_handle.add_data_in_tk_text(log_print_area_obj, f'{execute_result}')
                                current_time = os_handle.get_current_time()
                                tk_handle.add_data_in_tk_text(log_print_area_obj, f'Current Time: {current_time}\n')
                                return
                    print(f"fps_button_callback-----设备中不存在[{user_input_package_str}]包名")
                    tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中不存在[{user_input_package_str}]包名\n")
                    return
                thread_pool_obj.submit(fps_button_callback_execute_thread)
    fps_button = tk_handle.create_ttk_button(tk_window, "帧率信息",bootstyle=("dark", "outline"),width=9,callback=fps_button_callback)
    tk_handle.hand_tk_element_place(fps_button, 255, 220)

# 冷启动
def cold_run_app_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_package
    def cold_run_button_callback():
        if connect_status == False:
            print(f"cold_run_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if tk_handle.get_tk_string_value(user_input_package) == "":
                print(f"cold_run_button_callback-----请输入被测包名")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请输入被测包名\n")
            else:
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{tk_handle.get_tk_string_value(user_input_package)}]app冷启动中......\n')
                def cold_run_button_callback_execute_thread():
                    user_input_package_str = tk_handle.get_tk_string_value(user_input_package)
                    current_device_app_list = os_handle.execute_short_command( f'adb -s {connected_sn} shell "pm list packages -3"').split('\n')
                    for app_package in current_device_app_list:
                        user_input_package_temp = "package:" + user_input_package_str
                        if user_input_package_temp == app_package:
                            get_main_activity_execute_command = 'adb -s ' + connected_sn + ' shell \"dumpsys package ' + user_input_package_str + ' | grep android.intent.action.MAIN: -A 1 | tail -n +2 | awk -F \' \' \'{print $2}\'\" '
                            print(f"cold_run_button_callback-----get_main_activity_execute_command={get_main_activity_execute_command}")
                            get_main_activity_execute_result = None
                            get_main_activity_execute_result = os_handle.execute_short_command(get_main_activity_execute_command)
                            print(f"cold_run_button_callback-----get_main_activity_execute_result={get_main_activity_execute_result}")
                            if get_main_activity_execute_result != None:
                                run_main_activity_execute_command = f'adb -s {connected_sn} shell "am start -W {get_main_activity_execute_result}"'
                                print(f"cold_run_button_callback-----run_main_activity_execute_command={run_main_activity_execute_command}")
                                u2_handle.close_apk(connected_object, user_input_package_str)
                                time.sleep(3)
                                run_main_activity_execute_result = None
                                run_main_activity_execute_result = os_handle.execute_short_command(run_main_activity_execute_command)
                                print(f"cold_run_button_callback-----run_main_activity_execute_result={run_main_activity_execute_result}")
                                if run_main_activity_execute_result != None:
                                    print(f"cold_run_button_callback-----启动[{user_input_package_str}]主Activity成功")
                                    tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app冷启动信息如下:\n')
                                    tk_handle.add_data_in_tk_text(log_print_area_obj, f'{run_main_activity_execute_result}\n')
                                    return
                                else:
                                    print(f"cold_run_button_callback-----启动[{user_input_package_str}]主Activity失败")
                                    tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app冷启动失败:\n')
                                    tk_handle.add_data_in_tk_text(log_print_area_obj, f'{run_main_activity_execute_result}\n')
                                    return
                            else:
                                print(f"cold_run_button_callback-----获取[{user_input_package_str}]主Activity失败")
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取[{user_input_package}]主Activity失败\n')
                                return
                    print(f"cold_run_button_callback-----设备中不存在[{user_input_package_str}]包名")
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中不存在[{user_input_package_str}]包名\n")
                    return
                thread_pool_obj.submit(cold_run_button_callback_execute_thread)
    cold_run_button = tk_handle.create_ttk_button(tk_window, "冷启动",bootstyle=("dark", "outline"),width=9,callback=cold_run_button_callback)
    tk_handle.hand_tk_element_place(cold_run_button, 380, 220)


# 热启动
def hot_run_app_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_package
    def hot_run_button_callback():
        if connect_status == False:
            print(f"hot_run_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if tk_handle.get_tk_string_value(user_input_package) == "":
                print(f"hot_run_button_callback-----请输入被测包名")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请输入被测包名\n")
            else:
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{tk_handle.get_tk_string_value(user_input_package)}]app热启动中......\n')
                def hot_run_button_callback_execute_thread():
                    user_input_package_str = tk_handle.get_tk_string_value(user_input_package)
                    current_device_app_list = os_handle.execute_short_command(f'adb -s {connected_sn} shell "pm list packages -3"').split('\n')
                    for app_package in current_device_app_list:
                        user_input_package_temp = "package:" + user_input_package_str
                        if user_input_package_temp == app_package:
                            get_main_activity_execute_command = 'adb -s ' + connected_sn + ' shell \"dumpsys package ' + user_input_package_str + ' | grep android.intent.action.MAIN: -A 1 | tail -n +2 | awk -F \' \' \'{print $2}\'\" '
                            print(f"hot_run_button_callback-----get_main_activity_execute_command={get_main_activity_execute_command}")
                            get_main_activity_execute_result = None
                            get_main_activity_execute_result = os_handle.execute_short_command(get_main_activity_execute_command)
                            print(f"hot_run_button_callback-----get_main_activity_execute_result={get_main_activity_execute_result}")
                            if get_main_activity_execute_result != None:
                                run_main_activity_execute_command = f'adb -s {connected_sn} shell "am start -W {get_main_activity_execute_result}"'
                                print(f"hot_run_button_callback-----run_main_activity_execute_command={run_main_activity_execute_command}")
                                u2_handle.open_apk(connected_object, user_input_package_str)
                                u2_handle.press_home_key(connected_object)
                                time.sleep(3)
                                run_main_activity_execute_result = None
                                run_main_activity_execute_result = os_handle.execute_short_command(run_main_activity_execute_command)
                                print(f"hot_run_button_callback-----run_main_activity_execute_result={run_main_activity_execute_result}")
                                if run_main_activity_execute_result != None:
                                    print(f"hot_run_button_callback-----启动[{user_input_package_str}]主Activity成功")
                                    tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app热启动信息如下:\n')
                                    tk_handle.add_data_in_tk_text(log_print_area_obj, f'{run_main_activity_execute_result}\n')
                                    return
                                else:
                                    print(f"hot_run_button_callback-----启动[{user_input_package_str}]主Activity失败")
                                    tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app热启动失败:\n')
                                    tk_handle.add_data_in_tk_text(log_print_area_obj, f'{run_main_activity_execute_result}\n')
                                    return
                            else:
                                print(f"hot_run_button_callback-----获取[{user_input_package_str}]主Activity失败")
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备获取[{user_input_package_str}]主Activity失败\n')
                                return
                    print(f"hot_run_button_callback-----设备中不存在[{user_input_package_str}]包名")
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中不存在[{user_input_package_str}]包名\n")
                    return
                thread_pool_obj.submit(hot_run_button_callback_execute_thread)
    hot_run_button = tk_handle.create_ttk_button(tk_window, "热启动",bootstyle=("dark", "outline"),width=9,callback=hot_run_button_callback)
    tk_handle.hand_tk_element_place(hot_run_button, 505, 220)


# 流量信息
def get_app_net_info_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_package
    def net_button_callback():
        if connect_status == False:
            print(f"net_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if tk_handle.get_tk_string_value(user_input_package) == "":
                print(f"net_button_callback-----请输入被测包名")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请输入被测包名\n")
            else:
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{tk_handle.get_tk_string_value(user_input_package)}]app获取流量信息中......\n')
                def net_button_callback_execute_thread():
                    user_input_package_str = tk_handle.get_tk_string_value(user_input_package)
                    current_device_app_list = os_handle.execute_short_command(f'adb -s {connected_sn} shell "pm list packages -3"').split('\n')
                    for app_package in current_device_app_list:
                        user_input_package_temp = "package:" + user_input_package_str
                        if user_input_package_temp == app_package:
                            query_pid_command = 'adb -s ' + connected_sn + ' shell \" ps -elf | grep ' +  user_input_package_str + ' | grep -v grep | head -n 1 | awk -F \' \' \'{print $2}\'\"'
                            print(f"net_button_callback-----query_pid_command={query_pid_command}")
                            query_pid_execute_result = os_handle.execute_short_command(query_pid_command)
                            print(f"net_button_callback-----query_pid_execute_result={query_pid_execute_result}")
                            if query_pid_execute_result == "":
                                print(f"net_button_callback-----[{user_input_package_str}]程序不在运行状态")
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中[{user_input_package_str}]app不在运行状态\n")
                                return
                            else:
                                app_pid = query_pid_execute_result.rstrip()
                                print(f"net_button_callback-----app_pid={app_pid}")
                                query_net_execute_command = 'adb -s ' + connected_sn + ' shell \" cat /proc/' + app_pid + '/net/dev | tail -n +3 | awk -F \' \' \'{print $1,$2,$3,$10,$11}\'\"'
                                print(f"net_button_callback-----query_net_execute_command={query_net_execute_command}")
                                query_net_execute_command_execute_result = os_handle.execute_short_command(query_net_execute_command)
                                print(f"net_button_callback-----query_net_execute_command_execute_result={query_net_execute_command_execute_result}")
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app流量信息如下:\n')
                                print(f'        receive                              send')
                                print(f'face           bytes          packets        bytes          packets')
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f'               Receive                       Send\n')
                                tk_handle.add_data_in_tk_text(log_print_area_obj,f'face           bytes          packets        bytes          packets\n')
                                # # test.ljust(20, '*') 左对齐 20宽度 不足以*填充
                                show_datas = query_net_execute_command_execute_result.split("\n")
                                print(f"net_button_callback-----show_data={show_datas}")
                                for row_data in show_datas:
                                    trimmed_text = re.sub(r"\s+", " ", row_data.strip())
                                    row_data = trimmed_text.split(" ")
                                    if row_data[0] != '':
                                       print( row_data[0].ljust(15, ' ') + row_data[1].ljust(15, ' ') + row_data[2].ljust(15,' ') +row_data[3].ljust(15, ' ') + row_data[4].ljust(15, ' '))
                                       tk_handle.add_data_in_tk_text(log_print_area_obj, row_data[0].ljust(15, ' ') + row_data[1].ljust(15, ' ') + row_data[2].ljust(15, ' ') + row_data[3].ljust(15, ' ') + row_data[4].ljust(15, ' ') + '\n')
                                return
                    print(f"net_button_callback-----设备中不存在[{user_input_package_str}]包名")
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中不存在[{user_input_package_str}]包名\n")
                    return
                thread_pool_obj.submit(net_button_callback_execute_thread)
    net_button = tk_handle.create_ttk_button(tk_window, "流量信息",bootstyle=("dark", "outline"),width=9,callback=net_button_callback)
    tk_handle.hand_tk_element_place(net_button, 630, 220)



# monkey子窗口部分 ============================================================================================================
# 初始化monkey子窗口大小
def set_monkey_child_window(tk_monkey_child_window):
    global log_print_area_obj
    global user_selected_device_sn

    tk_handle.set_window_title(tk_monkey_child_window, "Monkey")
    tk_handle.set_window_size(tk_monkey_child_window, 355, 450,400, 200)
    tk_handle.set_window_size_lock_status(tk_monkey_child_window, False, False)
    # tk_handle.set_window_icon(tk_monkey_child_window, ".\\1.ico")
    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey参数配置窗口已打开\n")

# monkey子窗口参数标签
def monkey_para_label_implement(tk_monkey_child_window):
    monkey_para_label = tk_handle.create_ttk_label(tk_monkey_child_window, "Monkey参数设置", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(monkey_para_label, 100, 5)

# monkey子窗口触摸事件
def touch_event_implement(tk_monkey_child_window):
    global user_input_touch_event_percent
    touch_event_label = tk_handle.create_ttk_label(tk_monkey_child_window, "点击事件占比:", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(touch_event_label, 10, 40)

    user_input_touch_event_percent = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_touch_event_percent, 0)
    touch_event_input_entry = tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=monkey_para_percent, bind_str=user_input_touch_event_percent,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(touch_event_input_entry, 200, 40)

# monkey子窗口水平滑动事件
def straight_slide_event_implement(tk_monkey_child_window):
    global user_input_straight_slide_event_percent
    straight_slide_event_label = tk_handle.create_ttk_label(tk_monkey_child_window, "水平滑动事件占比:", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(straight_slide_event_label, 10, 75)

    user_input_straight_slide_event_percent = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_straight_slide_event_percent,0)
    straight_slide_event_input_entry = tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=monkey_para_percent, bind_str=user_input_straight_slide_event_percent,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(straight_slide_event_input_entry, 200,75)

# 曲线滑动事件
def bend_slide_event_implement(tk_monkey_child_window):
    global user_input_bend_slide_event_percent
    bend_slide_event_label = tk_handle.create_ttk_label(tk_monkey_child_window, "曲线滑动事件占比:", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(bend_slide_event_label, 10, 110)

    user_input_bend_slide_event_percent = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_bend_slide_event_percent,0)
    bend_slide_event_input_entry =  tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=monkey_para_percent, bind_str=user_input_bend_slide_event_percent,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(bend_slide_event_input_entry, 200, 110)

# 屏幕缩放事件
def window_small_event_implement(tk_monkey_child_window):
    global user_input_window_small_event_percent
    window_small_event_label = tk_handle.create_ttk_label(tk_monkey_child_window, "屏幕缩放事件占比:", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(window_small_event_label, 10, 145)

    user_input_window_small_event_percent = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_window_small_event_percent,0)
    window_small_event_input_entry = tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=monkey_para_percent, bind_str=user_input_window_small_event_percent,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(window_small_event_input_entry, 200,145)

# 屏幕旋转事件
def window_turn_event_implement(tk_monkey_child_window):
    global user_input_window_turn_event_percent
    window_turn_event_label = tk_handle.create_ttk_label(tk_monkey_child_window, "屏幕旋转事件占比:", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(window_turn_event_label, 10, 180)

    user_input_window_turn_event_percent = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_window_turn_event_percent,0)
    window_turn_event_input_entry = tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=monkey_para_percent, bind_str=user_input_window_turn_event_percent,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(window_turn_event_input_entry, 200, 180)

# 系统按键事件
def system_key_event_implement(tk_monkey_child_window):
    global user_input_system_key_event_percent
    system_key_event_label = tk_handle.create_ttk_label(tk_monkey_child_window, "系统按键事件占比:", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(system_key_event_label, 10, 215)

    user_input_system_key_event_percent = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_system_key_event_percent,0)
    system_key_event_input_entry = tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=monkey_para_percent, bind_str=user_input_system_key_event_percent,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(system_key_event_input_entry, 200, 215)


# 键盘输入事件
def keyboard_input_event_implement(tk_monkey_child_window):
    global user_input_keyboard_event_percent
    keyboard_event_label = tk_handle.create_ttk_label(tk_monkey_child_window, "键盘输入事件占比:", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(keyboard_event_label, 10, 250)

    user_input_keyboard_event_percent = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_keyboard_event_percent,0)
    keyboard_event_input_entry = tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=monkey_para_percent, bind_str=user_input_keyboard_event_percent,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(keyboard_event_input_entry, 200, 250)

# 切换页面事件
def switch_page_event_implement(tk_monkey_child_window):
    global user_input_switch_page_event_percent
    switch_page_event_label = tk_handle.create_ttk_label(tk_monkey_child_window, "切换页面事件占比:", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(switch_page_event_label, 10, 285)

    user_input_switch_page_event_percent = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_switch_page_event_percent,0)
    switch_page_event_input_entry = tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=monkey_para_percent, bind_str=user_input_switch_page_event_percent,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(switch_page_event_input_entry, 200, 285)

# 事件间隔
def event_timeout_implement(tk_monkey_child_window):
    global user_input_event_timeout
    event_timeout_label = tk_handle.create_ttk_label(tk_monkey_child_window, "时间间隔(毫秒):", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(event_timeout_label, 10, 320)

    user_input_event_timeout = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_event_timeout, 1000)
    event_timeout_input_entry = tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=event_timeout_time, bind_str=user_input_event_timeout,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(event_timeout_input_entry, 200, 320)

# 事件数
def event_count_implement(tk_monkey_child_window):
    global user_input_event_count
    event_counts_label = tk_handle.create_ttk_label(tk_monkey_child_window, "事件个数:", bootstyle="dark", text_size=10, width=20)
    tk_handle.hand_tk_element_place(event_counts_label, 10, 355)

    user_input_event_count = tk_handle.create_tk_string()
    tk_handle.set_tk_string_value(user_input_event_count, 100)
    event_counts_input_entry = tk_handle.create_ttk_dropbox(tk_monkey_child_window, options=event_number, bind_str=user_input_event_count,width=10,text_size=9,bootstyle=("dark"))
    tk_handle.hand_tk_element_place(event_counts_input_entry, 200, 355)


# monkey重置参数按钮
def reset_monkey_para(tk_monkey_child_window):
    def monkey_para_reset_button_callback():
        global log_print_area_obj
        global user_selected_device_sn
        print("monkey_para_reset_button_callback")
        tk_handle.set_tk_string_value(user_input_touch_event_percent, "0")
        tk_handle.set_tk_string_value(user_input_straight_slide_event_percent, "0")
        tk_handle.set_tk_string_value(user_input_bend_slide_event_percent, "0")
        tk_handle.set_tk_string_value(user_input_window_small_event_percent, "0")
        tk_handle.set_tk_string_value(user_input_window_turn_event_percent, "0")
        tk_handle.set_tk_string_value(user_input_system_key_event_percent, "0")
        tk_handle.set_tk_string_value(user_input_keyboard_event_percent, "0")
        tk_handle.set_tk_string_value(user_input_switch_page_event_percent, "0")

        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,[点击触摸事件]占比已初始化为 0\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,[水平滑动事件]占比已初始化为 0\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,[曲线滑动事件]占比已初始化为 0\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,[屏幕缩放事件]占比已初始化为 0\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,[屏幕旋转事件]占比已初始化为 0\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,[系统按键事件]占比已初始化为 0\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,[键盘输入事件]占比已初始化为 0\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,[切换页面事件]占比已初始化为 0\n")
    monkey_para_reset_button = tk_handle.create_ttk_button(tk_monkey_child_window, "重置占比", bootstyle=("dark"), width=8,callback=monkey_para_reset_button_callback)
    tk_handle.hand_tk_element_place(monkey_para_reset_button, 65, 400)



# monkey执行按钮
def start_monkey(tk_monkey_child_window,user_input_package_str):
    def monkey_execute_button_callback():
        global log_print_area_obj
        global user_selected_device_sn
        print("monkey_execute_button_callback")
        print(f"monkey_execute_button_callback-----点击触摸事件占比:{tk_handle.get_tk_string_value(user_input_touch_event_percent)}")
        print(f"monkey_execute_button_callback-----水平滑动事件占比:{tk_handle.get_tk_string_value(user_input_straight_slide_event_percent)}")
        print(f"monkey_execute_button_callback-----曲线滑动事件占比:{tk_handle.get_tk_string_value(user_input_bend_slide_event_percent)}")
        print(f"monkey_execute_button_callback-----屏幕缩放事件占比:{tk_handle.get_tk_string_value(user_input_window_small_event_percent)}")
        print(f"monkey_execute_button_callback-----屏幕旋转事件占比:{tk_handle.get_tk_string_value(user_input_window_turn_event_percent)}")
        print(f"monkey_execute_button_callback-----系统按键事件占比:{tk_handle.get_tk_string_value(user_input_system_key_event_percent)}")
        print(f"monkey_execute_button_callback-----键盘输入事件占比:{tk_handle.get_tk_string_value(user_input_keyboard_event_percent)}")
        print(f"monkey_execute_button_callback-----切换页面事件占比:{tk_handle.get_tk_string_value(user_input_switch_page_event_percent)}")
        print(f"monkey_execute_button_callback-----事件间隔:{tk_handle.get_tk_string_value(user_input_event_timeout)}")
        print(f"monkey_execute_button_callback-----事件个数:{tk_handle.get_tk_string_value(user_input_event_count)}")

        percent = (int(tk_handle.get_tk_string_value(user_input_touch_event_percent)) + int(tk_handle.get_tk_string_value(user_input_straight_slide_event_percent)) + int(tk_handle.get_tk_string_value(user_input_bend_slide_event_percent)) + int(tk_handle.get_tk_string_value(user_input_window_small_event_percent)) + int(tk_handle.get_tk_string_value(user_input_window_turn_event_percent)) + int(tk_handle.get_tk_string_value(user_input_system_key_event_percent)) + int(tk_handle.get_tk_string_value(user_input_keyboard_event_percent)) + int(tk_handle.get_tk_string_value(user_input_switch_page_event_percent)))

        if percent < 1 or percent > 100:
            print("各事件总和占比最小1,最大100")
            tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,各事件总和占比最小1,最大100,目前{percent},请重新设置占比\n")
            return

        global monkey_execute_status
        if monkey_execute_status != False:
            print('monkey测试正在执行中,请等待执行完毕，再次执行')
            tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试正在执行中,请等待执行完毕，再次执行\n")
            return

        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[点击触摸事件]占比:{tk_handle.get_tk_string_value(user_input_touch_event_percent)}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[水平滑动事件]占比:{tk_handle.get_tk_string_value(user_input_straight_slide_event_percent)}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[曲线滑动事件]占比:{tk_handle.get_tk_string_value(user_input_bend_slide_event_percent)}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[屏幕缩放事件]占比:{tk_handle.get_tk_string_value(user_input_window_small_event_percent)}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[屏幕旋转事件]占比:{tk_handle.get_tk_string_value(user_input_window_turn_event_percent)}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[系统按键事件]占比:{tk_handle.get_tk_string_value(user_input_system_key_event_percent)}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[键盘输入事件]占比:{tk_handle.get_tk_string_value(user_input_keyboard_event_percent)}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[切换页面事件]占比:{tk_handle.get_tk_string_value(user_input_switch_page_event_percent)}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[事件间隔ms]:{tk_handle.get_tk_string_value(user_input_event_timeout)}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试,设置[事件个数]:{tk_handle.get_tk_string_value(user_input_event_count)}\n")

        current_time = os_handle.get_current_time()

        if connected_sn.startswith("192.168."):
            monkey_log_save_path = f"D:\\{connected_sn[0:-5]}"
        else:
            monkey_log_save_path = f"D:\\{connected_sn}"
        if os_handle.check_dirfile_is_exist(monkey_log_save_path) == False:
            os_handle.make_dir(monkey_log_save_path)

        if connected_sn.startswith("192.168."):
            monkey_log_save_path = f"D:\\{connected_sn[0:-5]}\\{current_time}_monkey_log.txt"
        else:
            monkey_log_save_path = f"D:\\{connected_sn}\\{current_time}_monkey_log.txt"

        print(f"monkey_execute_button_callback-----点击触摸事件占比:{tk_handle.get_tk_string_value(user_input_touch_event_percent)}")
        print(f"monkey_execute_button_callback-----水平滑动事件占比:{tk_handle.get_tk_string_value(user_input_straight_slide_event_percent)}")
        print(f"monkey_execute_button_callback-----曲线滑动事件占比:{tk_handle.get_tk_string_value(user_input_bend_slide_event_percent)}")
        print(f"monkey_execute_button_callback-----屏幕缩放事件占比:{tk_handle.get_tk_string_value(user_input_window_small_event_percent)}")
        print(f"monkey_execute_button_callback-----屏幕旋转事件占比:{tk_handle.get_tk_string_value(user_input_window_turn_event_percent)}")
        print(f"monkey_execute_button_callback-----系统按键事件占比:{tk_handle.get_tk_string_value(user_input_system_key_event_percent)}")
        print(f"monkey_execute_button_callback-----键盘输入事件占比:{tk_handle.get_tk_string_value(user_input_keyboard_event_percent)}")
        print(f"monkey_execute_button_callback-----切换页面事件占比:{tk_handle.get_tk_string_value(user_input_switch_page_event_percent)}")
        print(f"monkey_execute_button_callback-----事件间隔:{tk_handle.get_tk_string_value(user_input_event_timeout)}")
        print(f"monkey_execute_button_callback-----事件个数:{tk_handle.get_tk_string_value(user_input_event_count)}")

        monkey_execute_command = (f'adb -s {connected_sn} shell "monkey --throttle {tk_handle.get_tk_string_value(user_input_event_timeout)} --pct-touch {tk_handle.get_tk_string_value(user_input_touch_event_percent)} --pct-motion {tk_handle.get_tk_string_value(user_input_straight_slide_event_percent)} --pct-trackball {tk_handle.get_tk_string_value(user_input_bend_slide_event_percent)} --pct-pinchzoom {tk_handle.get_tk_string_value(user_input_window_small_event_percent)} --pct-rotation {tk_handle.get_tk_string_value(user_input_window_turn_event_percent)} --pct-syskeys {tk_handle.get_tk_string_value(user_input_system_key_event_percent)} --pct-flip {tk_handle.get_tk_string_value(user_input_keyboard_event_percent)} -v -v -v -p {user_input_package_str} {tk_handle.get_tk_string_value(user_input_event_count)} --pct-appswitch {tk_handle.get_tk_string_value(user_input_switch_page_event_percent)} --ignore-crashes --ignore-timeouts --ignore-security-exception" > {monkey_log_save_path}')

        print(f"monkey_execute_button_callback-----monkey_execute_command={monkey_execute_command}")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试预执行命令:{monkey_execute_command}\n")
        tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试执行中......\n")
        monkey_execute_status = True

        def monkey_execute_button_callback_execute_thread():
            global monkey_execute_status
            execute_result = os_handle.execute_short_command(monkey_execute_command)
            print(f"monkey_execute_button_callback_execute_thread-----execute_result={execute_result}")
            monkey_execute_status = False
            tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试执行完毕,执行log保存在{monkey_log_save_path}\n")
            tk_handle.pop_err_tip(f"{tk_handle.get_tk_string_value(user_selected_device_sn)}设备Monkey", f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey测试执行完毕,执行log保存在{monkey_log_save_path}",method="info")
        thread_pool_obj.submit(monkey_execute_button_callback_execute_thread)

        def check_test_app_is_showing_thread():
            global monkey_execute_status
            while True:
                print(monkey_execute_status)
                if monkey_execute_status:
                    current_package = u2_handle.get_current_show_apk(connected_object)
                    print(f"check_test_app_is_showing_thread-----current_package={current_package['package']}")
                    u2_handle.open_apk(connected_object, user_input_package_str)
                else:
                    print(f"check_test_app_is_showing_thread-----monkey执行完毕了")
                    return
        thread_pool_obj.submit(check_test_app_is_showing_thread)

    monkey_execute_button = tk_handle.create_ttk_button(tk_monkey_child_window, "执行", bootstyle=("danger"), width=8,callback=monkey_execute_button_callback)
    tk_handle.hand_tk_element_place(monkey_execute_button, 180, 400)




# 设置monkey子窗口各元素
def set_monkey_child_element(tk_monkey_child_window):
    # monkey参数标签
    monkey_para_label_implement(tk_monkey_child_window)
    # 触摸事件
    touch_event_implement(tk_monkey_child_window)
    # 水平滑动事件
    straight_slide_event_implement(tk_monkey_child_window)
    # 曲线滑动事件
    bend_slide_event_implement(tk_monkey_child_window)
    # 屏幕缩放事件
    window_small_event_implement(tk_monkey_child_window)
    # 屏幕旋转事件
    window_turn_event_implement(tk_monkey_child_window)
    # 系统按键事件
    system_key_event_implement(tk_monkey_child_window)
    # 键盘输入事件
    keyboard_input_event_implement(tk_monkey_child_window)
    # 切换页面事件
    switch_page_event_implement(tk_monkey_child_window)
    # 事件间隔
    event_timeout_implement(tk_monkey_child_window)
    # 事件数
    event_count_implement(tk_monkey_child_window)

# Monkey测试
def monkey_test_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_package
    def monkey_button_callback():
        if connect_status == False:
            print(f"monkey_button_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if tk_handle.get_tk_string_value(user_input_package) == "":
                print(f"monkey_button_callback-----请输入被测包名")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请输入被测包名\n")
            else:
                tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备Monkey参数配置窗口打开中......\n')
                user_input_package_str = tk_handle.get_tk_string_value(user_input_package)
                current_device_app_list = os_handle.execute_short_command(f'adb -s {connected_sn} shell "pm list packages -3"').split('\n')
                for app_package in current_device_app_list:
                    user_input_package_temp = "package:" + user_input_package_str
                    if user_input_package_temp == app_package:
                        tk_monkey_child_window = tk_handle.create_child_tk_window()
                        tk_handle.set_window_focus(tk_monkey_child_window)
                        if tk_monkey_child_window != None:
                            # 初始化子窗口大小 位置
                            set_monkey_child_window(tk_monkey_child_window)
                            # # 初始化子窗口内元素大小 位置
                            set_monkey_child_element(tk_monkey_child_window)
                            # 初始化重置参数按钮
                            reset_monkey_para(tk_monkey_child_window)
                            # monkey执行按钮
                            start_monkey(tk_monkey_child_window,user_input_package_str)
                            # 展示子窗口
                            tk_handle.show_tk_window(tk_monkey_child_window)
                            return
                print(f"monkey_button_callback-----设备中不存在[{user_input_package_str}]包名")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中不存在[{user_input_package_str}]包名\n")
    monkey_button = tk_handle.create_ttk_button(tk_window, "Monkey",bootstyle=("dark", "outline"),width=9,callback=monkey_button_callback)
    tk_handle.hand_tk_element_place(monkey_button, 755, 220)


# 卸载app
def uninstall_app_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_package
    def uninstall_app_callback():
        if connect_status == False:
            print(f"uninstall_app_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if tk_handle.get_tk_string_value(user_input_package) == "":
                print(f"uninstall_app_callback-----请输入被测包名")
                tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请输入被测包名\n")
            else:
                 tk_handle.add_data_in_tk_text(log_print_area_obj,f'[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{tk_handle.get_tk_string_value(user_input_package)}]app卸载中......\n')
                 def uninstall_app_callback_execute_thread():
                     global user_input_package
                     user_input_package_str = tk_handle.get_tk_string_value(user_input_package)
                     print(f"uninstall_app_callback_execute_thread-----user_input_package_str={user_input_package_str}")
                     current_device_app_list = os_handle.execute_short_command(f'adb -s {connected_sn} shell "pm list packages -3"').split('\n')
                     print(f"uninstall_app_callback_execute_thread-----current_device_app_list={current_device_app_list}")
                     for app_package in current_device_app_list:
                         user_input_package_temp = "package:" + user_input_package_str
                         print(f"uninstall_app_callback_execute_thread----user_input_package_temp={user_input_package_temp}")
                         if user_input_package_temp == app_package:
                             print(f"uninstall_app_callback_execute_thread----用户输入的包名存在")
                             execute_command = f'adb -s {connected_sn} uninstall {user_input_package_str}'
                             print(f"uninstall_app_callback_execute_thread-----execute_command={execute_command}")
                             execute_result = os_handle.execute_short_command(execute_command)
                             print(f"uninstall_app_callback_execute_thread-----execute_result={execute_result}")
                             print(len(execute_result))
                             if "Success" in execute_result:
                                 print(f"uninstall_app_callback_execute_thread-----[{user_input_package_str}]程序卸载成功")
                                 tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app卸载成功\n")
                                 return
                             else:
                                 print(f"uninstall_app_callback_execute_thread-----[{user_input_package_str}]程序卸载失败")
                                 tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{user_input_package_str}]app卸载失败\n")
                                 return
                     print(f"uninstall_app_callback_execute_thread-----设备中不存在[{user_input_package_str}]包名")
                     tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备中不存在[{user_input_package_str}]包名\n")
                     return
                 thread_pool_obj.submit(uninstall_app_callback_execute_thread)
    app_uninstall_button = tk_handle.create_ttk_button(tk_window, "卸载app",bootstyle=("dark", "outline"),width=9,callback=uninstall_app_callback)
    tk_handle.hand_tk_element_place(app_uninstall_button, 5, 260)

# 安装app
def install_app_implement():
    global log_print_area_obj
    global user_selected_device_sn
    global user_input_package
    def install_app_callback():
        global app_installing_status
        if connect_status == False:
            print(f"install_app_callback-----当前未连接设备")
            tk_handle.add_data_in_tk_text(log_print_area_obj, f"当前未连接设备\n")
        else:
            if app_installing_status == False:
                app_installing_status = True
                def install_app_callback_execute_thread():
                    global app_installing_status
                    app_path = tk_handle.select_file_get_path()
                    if os_handle.get_fileext_in_path(app_path) == '.apk':
                        print(f"install_app_callback_execute_thread-----app_path={app_path}")
                        tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{app_path}]app安装中......\n")
                        execute_command = f'adb -s {connected_sn} install {app_path}'
                        print(f"install_app_callback_execute_thread-----execute_command={execute_command}")
                        execute_result = os_handle.execute_short_command(execute_command)
                        print(f"install_app_callback_execute_thread-----execute_result={execute_result}")
                        if "Success" in execute_result:
                            tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{app_path}]app安装成功\n")
                            app_installing_status = False
                            return
                        else:
                            tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]设备[{app_path}]app安装失败\n")
                            app_installing_status = False
                            return
                    tk_handle.add_data_in_tk_text(log_print_area_obj,f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请选择正确apk路径\n")
                    app_installing_status = False
                thread_pool_obj.submit(install_app_callback_execute_thread)
            else:
                tk_handle.add_data_in_tk_text(log_print_area_obj, f"[{tk_handle.get_tk_string_value(user_selected_device_sn)}]请等待上次app安装结果后,再进行安装\n")
    app_install_button = tk_handle.create_ttk_button(tk_window, "安装app",bootstyle=("dark", "outline"),width=9,callback=install_app_callback)
    tk_handle.hand_tk_element_place(app_install_button, 5, 130)


# 程序开始位置 ==============================================================
# 实现exe程序打包后在任意位置可以双击运行
init_exe_path()

# 创建代操作对象  四个handle对象 分别是对tk os uiautomator2 json库常用方法的封装
tk_handle = Sun_tkinter()
os_handle = Sun_os()
u2_handle = Sun_uiautomator2()
json_handle = Sun_json()

# 窗口部分 =====================================================
# 创建主tk窗口
tk_window = tk_handle.create_ttk_window()
# 创建窗口风格对象
tk_style = tk_handle.create_ttk_style()
# tk主窗口位置 大小等配置
init_main_tk_window_implement(tk_window,tk_style)


# 标签组件部分 ========================================================
# 选择设备标签元素配置
select_device_label_implement()
# 命令输入标签元素
command_input_label_implement()
# 包名输入标签
package_name_input_label_implement()


# 输入框组件部分 =========================================================
# 下拉列表选择设备元素配置
select_device_dropbox_implement()
# 按回车键执行命令
command_input_entry_implement()
# 包名输入框
package_name_input_entry_implement()


# 文本框组件部分 =========================================================
# tk界面操作的log打印区域
log_print_area_implement()



# # 按钮组件部分 =======================================================
# # 通用部分 =====================================
# 连接用户选择的设备
connect_device_implement()
# 截屏
shot_screen_implement()
# 清除设备的日志缓存
clear_device_log_buffer_implement()
# 开始抓全量日志
start_catch_log_implement()
# 停止抓全量日志
stop_catch_log_implement()
# 修改设备的只读权限
remount_readonly_file_system_implement()
# 清除设备的crash文件
clear_crash_file_implement()
# 获取设备的crash文件
get_crash_file_implement()
# 显示当前包名 页面 进程 信息
get_current_package_message_implement()
# 开始录屏
start_recording_implement()
# 停止录屏
stop_recording_implement()
# 获取设备的系统信息
get_device_system_info_implement()
# 获取设备的网络信息
get_device_net_info_implement()
# 获取设备的三方app包名信息
get_third_app_info_implement()
# 获取系统电池信息
get_device_battery_info_implement()
# 安装app
install_app_implement()

# app专项测试部分 =================================
# 获取指定包名的cpu信息
get_app_cpu_info_implement()
# 获取指定包名的内存信息
get_app_memory_info_implement()
# 获取指定包名的帧率信息
get_app_fps_info_implement()
# 冷启动指定包名app
cold_run_app_implement()
# 热启动指定包名app
hot_run_app_implement()
# 获取指定包名的流量信息
get_app_net_info_implement()
# 卸载指定包名app
uninstall_app_implement()
# 对目标包名app进行monkey测试
monkey_test_implement()


# 执行系统命令部分 ====================================
# 执行命令
start_execute_command_implement()
# 停止执行命令
stop_execute_command_implement()


# 日志部分 ============================================
# 清除tk界面操作的log
clear_log_implement()
# 导出tk界面操作的log
export_log_implement()



# tk主窗口线程执行
tk_handle.show_tk_window(tk_window)
